package atomic.atomicrefrenearray;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicReferenceArray;

/**
 * @author ZhuQi
 * @ClassName Demo
 * @Date 2022/9/16 10:47
 * @Version 1.0
 * @Description 测试 AtomicReferenceArray
 */
@Slf4j
public class Demo {
    public static void main(String[] args) {
        AtomicReferenceArray<Resource> atomicReferenceArray = new AtomicReferenceArray<>(new Resource[10]);

        // 获取下标为 0 的引用
        Resource resource = atomicReferenceArray.get(0);
        log.info("resource:{}", resource);

        // 比较替换下标为 0 的地方的数据
        boolean b = atomicReferenceArray.compareAndSet(0, null, new Resource("张三","haha"));
        log.info("b:{} resource:{} ", b, atomicReferenceArray.get(0).toString());

        // 设置下标为 1 的地方的数据
        atomicReferenceArray.set(1, new Resource("李四","xixi"));
        log.info("resource:{} ", b, atomicReferenceArray.get(1).toString());
    }
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Resource {
    private String name;

    private String data;
}
